package com.tambapps.p2p.fandem.model

import com.tambapps.p2p.fandem.util.FileUtils
import java.io.File
import java.io.IOException
import java.util.Optional

open class FileData(var fileName: String? = null,
                    var fileSize: Long = 0,

    // nullable
                    var checksum: String? = null) {

    fun getChecksum(): Optional<String> {
        return Optional.ofNullable(checksum)
    }

    companion object {
        @JvmOverloads
        @Throws(IOException::class)
        fun fromFile(file: File, withChecksum: Boolean = true): FileData {
            val checksum: String? = if (withChecksum) {
                FileUtils.computeChecksum(file)
            } else {
                null
            }
            return FileData(file.name, file.length(), checksum)
        }
    }
}
